<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Approval extends Model
{
    use HasFactory;
    protected $fillable = ['company_id','project_id','role_id','admin_id','flow_id','model','source','source_id','title','content','status','current_node_id','current_record_id','created_by','approved_at','rejected_at','level_count'];
    const POSTONLY = ['company_id','project_id','role_id','admin_id','flow_id','model','source','source_id','title','content','status','current_node_id','current_record_id','created_by','approved_at','rejected_at','level_count'];

    const MODELS = [
        ['label'=>'项目立项','value'=>'Project'],
        ['label'=>'劳务公司','value'=>'LaborCompany'],
        ['label'=>'劳务班组','value'=>'LaborTeam'],
        ['label'=>'劳务人员','value'=>'Labor'],
        ['label'=>'采购单','value'=>'Purchase'],
    ];

    protected $appends = [
        'status_text'
    ];

    public function getStatusTextAttribute()
    {
        if($this->status == 'afoot'){
            return '审批中';
        }
        if($this->status == 'approved'){
            return '已通过';
        }
        if($this->status == 'rejected'){
            return '已驳回';
        }
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','name');
    }

    public function created_user()
    {
        return $this->hasOne(Admin::class,'id','created_by')->select('id','name');
    }

}